package com.tj.jinzao.activity;


import android.content.Intent;

import com.netease.nim.uikit.business.session.activity.P2PMessageActivity;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.model.CommentBean;
import com.tj.jinzao.model.ResultResponse;

import java.util.HashMap;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 聊天
 * Created by LiuYX on 2019/4/19.
 */
public class ChatActivity extends P2PMessageActivity {
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
    }
//用户分配发送消息
    @Override
    public void setMessage(String nickName,String userId) {
        Intent to=new Intent();
        to.setClass(this, ZXSActivity.class);
        to.putExtra("nickName",nickName);
        to.putExtra("userId",userId);
        startActivity(to);

    }
//用户评论
    @Override
    public void setCommit(long uid, String counsoler, String toUserId) {
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("uid",uid);
        hashMap.put("counsoler",counsoler);
        hashMap.put("star",toUserId);
        addSubscription(mApiService.getComment(hashMap), new SubscriberCallBack<CommentBean>(this) {
            @Override
            protected void onSuccess(CommentBean list) {

            }

            @Override
            protected void onFailure(ResultResponse response) {

            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });

    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

}
